<template>
  <div>
    <div align="right" style="margin-bottom: 15px">
      <el-button type="primary" @click="openAdd">新增基层</el-button>
    </div>
    <el-table :data="tableData" height="503" border>
      <el-table-column prop="code" label="基层编号" align="center" />
      <el-table-column prop="level" label="基层类型" align="center" />
      <el-table-column prop="name" label="基层名称" align="center" />
      <el-table-column prop="devcount" label="设备数量" align="center" />
      <el-table-column prop="usercount" label="员工数量" align="center" />
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-button type="text" @click="editOpen(scope.row)">
            编辑基层信息
          </el-button>
          <el-button type="text" @click="scanHospital(scope.row)">
            查看基层
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page.sync="page.current"
      :page-sizes="[10, 20, 30, 40, 50, 60, 70, 80, 90]"
      :page-size.sync="page.limit"
      layout="sizes, prev, pager, next ,total"
      :total="page.total"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      :title="title"
      :visible.sync="editVisible"
      width="550px"
      custom-class="edit-dialog"
      center
    >
      <el-form
        ref="editForm"
        :model="editForm"
        label-width="80px"
        style="padding: 0 20px 0px 0px"
        :rules="editRule"
      >
        <el-form-item label="机构名称" prop="hospitalName">
          <el-input v-model="editForm.hospitalName" />
        </el-form-item>
        <el-form-item label="机构编码" prop="code">
          <el-input
            v-model="editForm.code"
            placeholder="系统自动生成"
            disabled
          />
        </el-form-item>
        <el-form-item label="机构类型">
          <el-select v-model="editForm.level" style="width: 100%">
            <el-option value="一级丙等医院" />
            <el-option value="一级乙等医院" />
            <el-option value="一级甲等医院" />
            <el-option value="二级丙等医院" />
            <el-option value="二级乙等医院" />
            <el-option value="二级甲等医院" />
            <el-option value="三级丙等医院" />
            <el-option value="三级乙等医院" />
            <el-option value="三级甲等医院" />
            <el-option value="三级特等医院" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm()"> 确 定 </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import {
  getBasicHospitalListNew,
  savehospital,
  updatehospital
} from '@/api/back-management/user-manage/hospital-bottom'
export default {
  name: 'HospitalBottom',
  props: {
    hospitalId: {
      type: String,
      default: '37'
    },
    regionId: {
      type: String,
      default: '0'
    }
  },
  data() {
    return {
      tableData: [],
      editVisible: false,
      editForm: {
        hospitalName: '',
        level: ''
      },
      title: '',
      page: {
        current: 1,
        limit: 10,
        total: 0
      },
      editRule: {
        hospitalName: [{ required: true, message: '请输入机构名称', trigger: 'blur' }],
        level: [{ required: true, message: '请输入机构类型', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getBasicHospitalListNew()
  },
  methods: {
    getBasicHospitalListNew() {
      getBasicHospitalListNew({
        token: getToken(),
        hospitalId: this.hospitalId
          ? this.hospitalId
          : this.$store.getters.hospitalId,
        pageNum: this.page.current,
        pageSize: this.page.limit
      }).then((res) => {
        if (!(typeof res === 'string' && res.indexOf('云服务接口错误') > -1)) {
          this.tableData = res.list
          this.page.total = res.total
        }
      })
    },
    handleSizeChange(val) {
      this.page.limit = val
      this.page.current = 1
      this.getHospitalListNew()
    },
    handleCurrentChange(val) {
      this.page.current = val
      this.getHospitalListNew()
    },
    openAdd() {
      this.editForm = {}
      this.title = '新增医院机构'
      this.editVisible = true
    },
    editOpen(row) {
      // this.editForm = JSON.parse(JSON.stringify(row))
      this.title = '修改医院机构'
      this.editForm.id = row.id
      this.editForm.hospitalName = row.name
      this.editForm.hospitalId = row.id
      this.editForm.parentId = row.parentId
      this.editForm.hospitalType = '2'
      this.editForm.level = row.level
      this.editForm.affiliated = true
      this.editForm.code = row.code
      this.editVisible = true
    },
    confirm() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.editForm.token = getToken()
          if (this.editForm.id) {
            this.edit()
          } else {
            this.editForm.phospitalId = this.hospitalId
              ? this.hospitalId
              : this.$store.getters.hospitalId

            this.editForm.regionId = this.regionId
              ? this.regionId
              : this.$store.getters.regionId
            this.editForm.parentId = 'root'
            this.editForm.hospitalType = '2'
            this.editForm.affiliated = true
            this.add()
          }
        }
      })
    },
    add() {
      savehospital(this.editForm).then((res) => {
        if (typeof res === 'string' && res.indexOf('云服务接口错误') > -1) {
          let err_message = '用户没有权限添加医院，请联系超级管理员！'
          if (res.indexOf(err_message) > -1) {
            this.$message.error(err_message)
          } else {
            this.$message.error('新增医院机构失败')
          }
        } else {
          this.$message.success('新增医院机构成功')
          this.getBasicHospitalListNew()
          this.editVisible = false
        }
      })
    },
    edit() {
      updatehospital(this.editForm).then((res) => {
        if (typeof res === 'string' && res.indexOf('云服务接口错误') > -1) {
          let err_message = '用户没有权限修改医院，请联系超级管理员！'
          if (res.indexOf(err_message) > -1) {
            this.$message.error(err_message)
          } else {
            this.$message.error('修改医院机构失败')
          }
        } else {
          this.$message.success('修改医院机构成功')
          this.getBasicHospitalListNew()
          this.editVisible = false
        }
      })
    },
    // 查看医院
    scanHospital(row) {
      row.toPage = 'basic'
      row.childTitle = row.name
      row.data = {
        hospitalId: row.id.toString(),
        regionId: row.regionId.toString()
      }
      this.$emit('child2Far', row)
    }
  }
}
</script>

<style lang="scss">
.edit-dialog {
  .el-dialog__body {
    padding-bottom: 0;
  }
}
</style>
